package com.jeomo.sys.provider.web;

import com.jeomo.common.core.dto.ITree;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.util.StringUtils;
import com.jeomo.common.core.util.TreeUtil;
import com.jeomo.shiro.util.LoginSessionUtil;
import com.jeomo.sys.api.vo.RoleVo;
import com.jeomo.sys.provider.request.CreateRoleRequest;
import com.jeomo.sys.provider.request.EditRoleRequest;
import com.jeomo.sys.provider.request.SaveRolePermissionsRequest;
import com.jeomo.sys.provider.web.vo.RoleTreeVo;
import com.jeomo.sys.starter.entity.Role;
import com.jeomo.sys.starter.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("sys/role")
public class RoleController {

    @Resource
    IRoleService roleService;

    @GetMapping("tree")
    public CommonResult<List<ITree>> tree() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        String topOrgCode = LoginSessionUtil.getCurrentLoginInfo().getTopOrgCode();
        List<Role> list = roleService.list();
        List<ITree> treeVo = BeanCopyUtil.copyListProperties(list, RoleTreeVo::new);
        // 将默认分组的代码设置为当前登录用户的顶级组织，确保创建在默认分组下的组织的上级组织和自己的顶级组织保持一致
        return CommonResult.success(TreeUtil.buildTree(treeVo));
    }

    @GetMapping("group/tree")
    public CommonResult<List<ITree>> groupTree() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        String topOrgCode = LoginSessionUtil.getCurrentLoginInfo().getTopOrgCode();
        List<Role> list = roleService.listGroup(topOrgCode);
        List<ITree> treeVo = BeanCopyUtil.copyListProperties(list, RoleTreeVo::new);
        return CommonResult.success(TreeUtil.buildTreeWithDefaultGroup(treeVo, topOrgCode, RoleTreeVo.class));
    }

    @GetMapping("/{code}")
    public CommonResult<RoleVo> info(@PathVariable("code") String code) {
        Role role = roleService.getById(code);
        return CommonResult.success(BeanCopyUtil.copyProperties(role, RoleVo.class));
    }

    @PostMapping("/saveRolePermissions")
    public boolean saveRolePermissions(@RequestBody SaveRolePermissionsRequest request) {
        return roleService.savePermissions(request.getRoleCode(), request.getPermissions());
    }

    /**
     * 新建组织
     * @return
     */
    @PostMapping("")
    public CommonResult<RoleVo> create(@RequestBody CreateRoleRequest request) {
        Role role = BeanCopyUtil.copyProperties(request, Role.class);
        String topOrgCode = LoginSessionUtil.getCurrentLoginInfo().getTopOrgCode();
        role.setTopOrgCode(LoginSessionUtil.getCurrentLoginInfo().getTopOrgCode());
        if(StringUtils.isEmpty(role.getParentCode())) {
            role.setParentCode(topOrgCode);
        }
        roleService.save(role);
        return CommonResult.success(BeanCopyUtil.copyProperties(role, RoleVo.class));
    }

    /**
     * 新建组织
     * @return
     */
    @PutMapping("")
    public CommonResult<RoleVo> update(@RequestBody EditRoleRequest request) {
        Role role = BeanCopyUtil.copyProperties(request, Role.class);
        role.setTopOrgCode(LoginSessionUtil.getCurrentLoginInfo().getTopOrgCode());
        roleService.updateById(role);
        return CommonResult.success(BeanCopyUtil.copyProperties(role, RoleVo.class));
    }

}
